<template>
    <el-card>
        <el-tabs type="card" @tab-click="handleClick">
            <el-tab-pane label="全部客户">
                <el-input v-model="queryData.clientName" placeholder="客户名称" size="small"></el-input>
                <el-input v-model="queryData.empName" placeholder="归属人员" size="small"></el-input>
                <el-input v-model="queryData.unFollowNum" placeholder="未跟进天数" size="small"></el-input>
                <el-date-picker
                        size="small"
                        v-model="queryData.joinTime"
                        type="datetime"
                        placeholder="添加时间">
                </el-date-picker>
                <el-date-picker
                        size="small"
                        v-model="queryData.lossTime"
                        type="datetime"
                        placeholder="流失时间">
                </el-date-picker>&nbsp;&nbsp;
<!--                <el-divider direction="vertical"></el-divider>-->
                <el-button type="primary" @click="queryDataHandle(1, 10)" size="small">查询</el-button>
<!--                <el-divider direction="vertical"></el-divider>-->
                <el-button plain @click="resetQuery" size="small">重置</el-button>
            </el-tab-pane>

            <el-tab-pane label="我的客户">
                <el-input v-model="queryData.clientName" placeholder="客户名称" size="small"></el-input>
                <el-input v-model="queryData.unFollowNum" placeholder="未跟进天数" size="small"></el-input>
                <el-date-picker
                        size="small"
                        v-model="queryData.joinTime"
                        type="datetime"
                        placeholder="添加时间">
                </el-date-picker>
                <el-date-picker
                        size="small"
                        v-model="queryData.lossTime"
                        type="datetime"
                        placeholder="流失时间">
                </el-date-picker>&nbsp;&nbsp;
<!--                <el-divider direction="vertical"></el-divider>-->
                <el-button type="primary" @click="queryDataHandle(1, 10)" size="small">查询</el-button>
<!--                <el-divider direction="vertical"></el-divider>-->
                <el-button plain @click="resetQuery" size="small">重置</el-button>
            </el-tab-pane>
        </el-tabs>

        <el-divider></el-divider>

<!--        <el-button type="primary" @click="dialogFormVisible = true" icon="el-icon-bell" size="small">提醒设置</el-button>
        <el-divider direction="vertical"></el-divider>-->
        <el-button plain icon="el-icon-download" size="small" @click="exportClientHandle">导出</el-button>

        <div class="interspaceH"></div>

        <el-table :data="dataSource" style="width: 100%" :header-cell-style="{background:'#F3F4F7',color:'#555'}">
            <el-table-column prop="clientName" label="客户名称"></el-table-column>
            <el-table-column prop="empName" label="归属人员"></el-table-column>
            <el-table-column prop="joinTime" label="添加时间" :formatter="formatter"></el-table-column>
            <el-table-column prop="lastFollowTime" label="最后跟进时间" :formatter="formatter"></el-table-column>
            <el-table-column prop="lossTime" label="流失时间" :formatter="formatter"></el-table-column>
            <el-table-column prop="unFollowNum" label="未跟进天数" :formatter="formatter"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button @click="detailHandle(scope.row)" type="text" size="small">详情</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div class="interspaceH"></div>

        <el-pagination @size-change="queryDataHandle(pagePagination.currentPage,$event)"
                       @current-change="queryDataHandle($event,pagePagination.pageSize)"
                       :current-page="pagePagination.currentPage"
                       :page-sizes="pagePagination.pageSizes" :page-size="pagePagination.pageSize"
                       layout="total, sizes, prev, pager, next"
                       :total="pagePagination.total">
        </el-pagination>

        <el-dialog title="提醒设置" :visible.sync="dialogFormVisible" width="700px">
            <div class="tip">
                1、客户联系人将企业员工从通讯录中删除时，客户流失列表中会产生一条客户流失记录；<br/>
                2、当【客户流失提醒】被开启后，客户联系人删除企业员工后，企业员工会收到流失提醒，员工可点击【挽留他】 进行再次尝试添加好友<br/>
            </div>
            <el-form :model="remindData">
                <div class="remindClass">
                    <el-form-item label="员工提醒" :label-width="formLabelWidth">
                        <el-switch v-model="remindData.employRemind" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
                    </el-form-item>
                    <transition name="el-fade-in">
                        <div v-show="remindData.employRemind" class="el-zoom-in-top">
                            <el-form-item label="当" :label-width="formLabelWidth">
                                    <el-select v-model="remindData.employ" placeholder="请选择" multiple>
                                        <el-option
                                                v-for="item in dictionary.stageList"
                                                :key="item.value"
                                                :label="item.label"
                                                :value="item.value">
                                        </el-option>
                                    </el-select>
                                    <span> 客户删除企业员工时，进行消息提醒</span>
                            </el-form-item>
                        </div>
                    </transition>
                </div>
                <div class="remindClass">
                    <el-form-item label="上级提醒" :label-width="formLabelWidth">
                        <el-switch v-model="remindData.superiorRemind" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
                    </el-form-item>
                    <transition name="el-fade-in">
                        <div v-show="remindData.superiorRemind" class="el-zoom-in-top">
                            <el-form-item label="当" :label-width="formLabelWidth">
                                <el-select v-model="remindData.superior" placeholder="请选择" multiple>
                                    <el-option
                                            v-for="item in dictionary.stageList"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                                <span> 客户删除企业员工时，消息提醒该员工上级</span>
                            </el-form-item>
                        </div>
                    </transition>
                </div>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="remindDetermine">确 定</el-button>
            </div>
        </el-dialog>

        <ClientDetail ref="clientDetail"/>
    </el-card>
</template>

<script>
import ClientDetail from '@/views/customer/client/client_detail'
const queryData = {
    clientName: '',//客户名称
    empName: '',//归属人员
    unFollowNum: '',//未跟进天数
    joinTime: '',//添加时间
    lossTime: '',//流失时间
}

export default {
    components:{
      ClientDetail,
    },
    data() {
        return {
            dialogFormVisible: false,
            formLabelWidth: '120px',
            remindData:{
                employRemind: true, //员工提醒
                employ:'',  // 客户删除提醒员工
                superiorRemind: true,   //上级提醒
                superior:'',    //客户删除提醒上级
            },
            tablePage: 0,
            detailVisible: false,
            dictionary: {
                stageList: []
            },
            fieldType: {
                joinTime: 'datetime',
                lossTime: 'datetime',
                lastFollowTime: 'datetime'
            },
            queryData: {...queryData},
            tabsIndex: 0,
            dataSource:[],
            pagePagination: {
                currentPage: 1,
                pageSizes: [10, 15, 20, 25],
                pageSize: 10,
                total: 0
            },
        }
    },
    mounted() {
        this.queryDataHandle()
        this.queryStageHandle()
        this.handleClick({index:this.tabsIndex})
    },
    methods: {
        handleClick(tab, event){
            this.queryData = {...queryData}
            this.tabsIndex = tab.index;
            this.queryDataHandle()
        },
        queryStageHandle() {
            this.$request("/api/clientStage/queryClientStageByParams/1_0", {}, "POST").then((data) => {
                let stageList = []
                data.rows.map((item) => {
                    stageList.push({label: item.clientStatusName, value: item.clientStatusId})
                })
                this.dictionary.stageList = stageList
            })
        },
        remindDetermine(){
            this.dialogFormVisible = false
            this.$message({
                message: '恭喜你，设置成功！',
                type: 'success'
            });
        },
        queryDataHandle(page = 1, row = 10) {
            let that = this;

            let queryData = {...this.queryData}

            queryData['joinTime'] == "" ? delete queryData['joinTime'] : null
            queryData['lossTime'] == "" ? delete queryData['lossTime'] : null

            let payload = {
                ...queryData,
                clientType: '2',
                pageNum: page,
                pageSize: row
            }

            payload['empId'] = that.tabsIndex == '0' ? "" : that.$store.getters.getUserInfo.userId;

            that.$request("/api/customers/getCustomersByParams/1_0", payload, "POST").then((data) => {
                that.dataSource = data.rows
                that.pagePagination['total'] = data.total
                that.pagePagination['pageSize'] = data.row
            })
        },
        resetQuery() {
            this.queryData = {...queryData}
        },
        formatter(row, column) {
            let field = column.property;
            let value = row[field];
            return this.$utils.formatterForLable(field, value, this.fieldType, this[field]);
        },
        detailHandle(row) {
            this.$refs.clientDetail.open(true, row)
        },
        exportClientHandle() {
            let that = this
            let payload = {}
            if (that.tabsIndex == '0') {

            } else if (that.tabsIndex == '1') {

            }
            that.$requestFile("/api/clientLoss/exportClientLoss/1_0", payload, "流失客户.xlsx")
        }
    }
}
</script>

<style scoped>
.el-tab-pane .el-input {
    width: 200px;
}

.el-tab-pane .el-select {
    width: 200px;
}

.el-form-item__content .el-input {
    width: 100%;
}

.remindClass {
    height: 13vh;
}
</style>
